//
//  ActivityTextCell.m
//  TourPlay
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "ActivityTextCell.h"
#define WIDTH 8
#define HEIGHT 6


@interface ActivityTextCell ()

@property (nonatomic, retain) UIView *viewBack;;
@property (nonatomic, retain) UILabel *textL;

@end


@implementation ActivityTextCell
- (void)dealloc
{
    [_viewBack release];
    [_textL release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    self.viewBack = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 0, SCREEN_WIDTH - WIDTH * 2, 50)];
    //    self.viewBack.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.9f];
    self.viewBack.backgroundColor = [UIColor whiteColor];
        self.viewBack.layer.cornerRadius = 4.f;
    [self.contentView addSubview:self.viewBack];
    [_viewBack release];
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.viewBack.frame.size.width - 20, 30)];
    titleL.text = @"活动介绍";
    titleL.font = [UIFont systemFontOfSize:15.f];
    [self.viewBack addSubview:titleL];
    [titleL release];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, titleL.frame.size.height, self.viewBack.frame.size.width - 20, 1)];
    view.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [self.viewBack addSubview:view];
    [view release];
    
    self.textL = [[UILabel alloc] initWithFrame:CGRectMake(10, view.frame.origin.y + view.frame.size.height + 10, self.viewBack.frame.size.width - 20, 50)];
    self.textL.numberOfLines = 0;
    self.textL.font = [UIFont systemFontOfSize:15.f];
    
    self.textL.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
    [self.viewBack addSubview:self.textL];
    [_textL release];
    
    // 夜间模式
    [self addColorChangedBlock:^{
        
        self.viewBack.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        self.viewBack.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        
        titleL.normalTextColor = [ColorSingleton shareSingleton].cellTitleColor;
        titleL.nightTextColor = [ColorSingleton shareSingleton].cellTitleColor;
        
        view.normalBackgroundColor = [ColorSingleton shareSingleton].cellLineColor;
        view.nightBackgroundColor = [ColorSingleton shareSingleton].cellLineColor;
    }];
}
- (void)setActivityMod:(ActivityModel *)activityMod
{
    if (_activityMod != activityMod) {
        [_activityMod release];
        _activityMod = [activityMod retain];
    }
    self.textL.text = self.activityMod.text;
    
    if (0 != self.textL.text.length) {
        // 调整行间距
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.textL.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:12.f];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.textL.text length])];
        self.textL.attributedText = attributedString;
        [self.textL sizeToFit];
        [paragraphStyle release];
        [attributedString release];
        
    }
    
    CGRect rectHeight = self.viewBack.frame;
    rectHeight.size.height =  self.textL.frame.size.height + 50     ;
    self.viewBack.frame = rectHeight;
    
    
}
+ (CGFloat)getTextHeigth:(ActivityModel *)activityMod
{
    UILabel *textL = [[UILabel alloc] initWithFrame:CGRectMake(10, 30 + 1 + 10, (SCREEN_WIDTH - WIDTH * 2) - 20, 50)];
    textL.numberOfLines = 0;
    // 去掉字符串尾部的空格
    textL.text = [activityMod.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    textL.font = [UIFont systemFontOfSize:15.f];
    
    if (0 != textL.text.length) {
        // 调整行间距
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textL.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:12.f];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textL.text length])];
        textL.attributedText = attributedString;
        [textL sizeToFit];
        [paragraphStyle release];
        [attributedString release];
        
    }
    
    CGFloat height = textL.frame.origin.y + textL.frame.size.height + 50;
    
    [textL release];
    return height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
